package ac.leedsmet.devmanager.core;

import ac.leedsmet.devmanager.core.persistence.FileStorage;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

/**
 * This class is creating a a new project and after the project creation the auto increment method
 * is giving a unique ID on the created project..
 * @author Samael
 * @version 1.0
 */
public class ProjectManagerImpl implements ProjectManager {

    protected static final Logger log = Logger.getLogger(IssueManagerImpl.class);
    private static long projectId = 100000L;
    private List<Project> projects = new LinkedList<Project>();

    /**
     * Creating a project and providing an auto incremented and unique id for it
     * @param title of the created project
     * @return  project object
     */
    @Override
    public Project createProject(String title) {
        projectId++;
        Project project = new Project(title, projectId);
        projects.add(project);
        return project;
    }

    /**
     * @return The list of the created projects
     */
    @Override
    public List<Project> getProjects() {
        return projects;
    }

    /**
     * @param projectId The project id of the Project object that is required
     * @return Returns the list of issues for the specified project.
     */
    public List<Issue> getIssuesForProject(final Long projectId) {
        if(projectId == null)
            throw new NullPointerException("null project Id");
        for(Project project : projects) {
            if(project.getProjectId() == projectId)
                return project.getIssues();
        }
        throw new NullPointerException("No issue found for project Id: " + projectId);
    }

    /**
     * @param projectId The project id of the Project object that is required
     * @return Returns the list of stories for the specified project.
     */
    public List<Story> getStoriesForProject(final Long projectId) {
        if(projectId == null)
            throw new NullPointerException("null project Id");

        for(Project project : projects) {
            if(project.getProjectId() == projectId)
                return project.getStories();
        }

        throw new NullPointerException("No project found for project Id: " + projectId);
    }

    public void saveProjectsToFile(String filename) throws IOException {
        FileStorage.saveObjectToFile(projects, filename);
    }

    @SuppressWarnings({"unchecked"})
    public void loadProjectsFromFile(String filename) throws IOException, ClassNotFoundException {
        projects = (List<Project>) FileStorage.loadObjectFromFile(filename);
    }

    public void exportProjectsToXmlFile(String filename) throws IOException {
        FileStorage.exportToXmlFile(projects, filename);
    }

    public void importProjectsFromXmlFile(String filename) throws ClassNotFoundException, IOException {
        projects = (List<Project>) FileStorage.importFromXmlFile(filename);
    }
}
